package org.tang.choose;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class ParseData {
	/**
	 * @param args
	 */
	 private static String urlpre =
	 "http://data.eastmoney.com/report/data.aspx?style=newylyc&field=IndustryID&bkcode=all&page=";
	 private static String urlpost = "&pageSize=50&sortType=A&sortRule=1";

//	private static String urlpre = "http://data.eastmoney.com/report/data.aspx?style=newylyc&field=IndustryID&bkcode=465&page=";
//	private static String urlpost = "&pageSize=50&sortType=A&sortRule=1";
	private static int pages = 50;
	private static ArrayList<String> array = new ArrayList<String>();

	public static void main(String[] args) {
		for (int i = 1; i < pages + 1; i++)
			handleUrl(i);
		String company = null;
		int total = 0;
		try {
			for (int i = 0; i < array.size(); i++) {
				company = array.get(i);
				String tmp[] = company.split(",", -2);
				if (!"".equals(tmp[10]) && !"".equals(tmp[12])
						&& !"".equals(tmp[14] )&& !"".equals(tmp[13])) {
					double t11 = Double.parseDouble(tmp[10]);
					double t12 = Double.parseDouble(tmp[12]);
					double t14 = Double.parseDouble(tmp[14]);
					double p12 = Double.parseDouble(tmp[13]);
					if (t11 > 0 && t11 < t12 && t12 * 1.5 < t14) {
						total++;
						System.out.println(company);
					}
				}
			}
			System.out.println("total = " + total);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(company);
		}
	}

	private static void handleUrl(int i) {
		try {
			URL url = new URL(urlpre + i + urlpost);
			URLConnection conn = url.openConnection();
			BufferedReader is = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			StringBuffer buffer = new StringBuffer();
			String str;
			while ((str = is.readLine()) != null) {
				buffer.append(str);
				buffer.append("\n");
			}
			int start = buffer.toString().indexOf(":[\"");
			int end = buffer.toString().indexOf(",]");
			handlerPage(buffer.toString().substring(start + 3, end - 1));
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void handlerPage(String page) {
		String tmp[] = page.split("\",\"");
		for (int i = 0; i < tmp.length; i++)
			array.add(tmp[i]);
	}
}